package com.mathassistant.client.presenter;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.event.ShowEditorEvent;
import com.mathassistant.client.event.exercise.ExerciseEditFinishedEvent;
import com.mathassistant.client.helper.RPCCall;
import com.mathassistant.client.service.ExerciseServiceAsync;
import com.mathassistant.shared.ExerciseDTO;
import com.mathassistant.shared.TopicDTO;

public class ExerciseEditPresenter implements Presenter {

	public interface Display
	{
		HasClickHandlers getQuestionAnchor();
		HasClickHandlers getAnswer1Anchor();
		HasClickHandlers getAnswer2Anchor();
		HasClickHandlers getAnswer3Anchor();
		HasClickHandlers getAnswer4Anchor();
		
		HasHTML getQuestionHtml();
		HasHTML getAnswer1Html();
		HasHTML getAnswer2Html();
		HasHTML getAnswer3Html();
		HasHTML getAnswer4Html();
		
		HasValue<Integer> getCorrectAnsIntBox();
		
		HasClickHandlers getSaveButton();
		HasClickHandlers getCloseButton();
		
		void setData(ExerciseDTO exercise);
		Widget asWidget();
	}
	
	private Display display;
	private final ExerciseServiceAsync rpcService;
	private TopicDTO topic;
	private ExerciseDTO exercise;
	private int indexBeginList = 0;
	private final SimpleEventBus eventBus;
	
	public ExerciseEditPresenter(ExerciseDTO exercise,
			TopicDTO topic, int indexBeginList,
			ExerciseServiceAsync rpcService,
			SimpleEventBus eventBus,
			Display view)
	{
		this.exercise = exercise;
		this.topic = topic;
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
		this.indexBeginList = indexBeginList;
		
		if(exercise!=null)
			display.setData(exercise);
		bind();
	}
	
	private void bind() {
		// TODO Auto-generated method stub
		display.getAnswer1Anchor().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new ShowEditorEvent(display.getAnswer1Html()));
			}
		});
		display.getAnswer2Anchor().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new ShowEditorEvent(display.getAnswer2Html()));
			}
		});
		display.getAnswer3Anchor().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new ShowEditorEvent(display.getAnswer3Html()));
			}
		});
		display.getAnswer4Anchor().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new ShowEditorEvent(display.getAnswer4Html()));
			}
		});
		
		display.getQuestionAnchor().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new ShowEditorEvent(display.getQuestionHtml()));
			}
		});
		
		display.getCloseButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO close
				doClose();
			}
		});
		
		display.getSaveButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO save
				doSave();
			}
		});
	}

	protected void doClose() {
		// TODO Auto-generated method stub
		eventBus.fireEvent(new ExerciseEditFinishedEvent(exercise, topic, indexBeginList));
	}

	protected void doSave() {
		// TODO Auto-generated method stub
		if(exercise == null){//them moi
			if(!CheckingInput()){
				return;
			}
			getExerciseFromDisplay();
			addNewExercise();
		} else {// cap nhat
			if(!CheckingInput()){
				return;
			}
			getExerciseFromDisplay();
			updateExercise();
		}
	}
	
	private boolean CheckingInput(){
		boolean result = false;
		int temp;
		try {
			temp = display.getCorrectAnsIntBox().getValue();
		} catch (Exception e) {
			temp = 0;
		}
		if(temp < 1 || 4 < temp)
		{
			Window.alert("Nhập đáp án không hợp lệ.");
			result = false;
		} else {
			result = true;
		}
		return result;
	}
	
	private void updateExercise() {
		// TODO Auto-generated method stub
		new RPCCall<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("Save exercise error caught: " + caught.getMessage());
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				Window.alert("Cập nhật thành công.");
				doClose();
			}

			@Override
			protected void callService(AsyncCallback<Void> cb) {
				// TODO Auto-generated method stub
				rpcService.updateExercise(exercise, cb);
			}
		}.retry(3);
	}

	private void addNewExercise() {
		// TODO Auto-generated method stub
		new RPCCall<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("Save exercise error caught: " + caught.getMessage());
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				Window.alert("Thêm thành công.");
				doClose();
			}

			@Override
			protected void callService(AsyncCallback<Void> cb) {
				// TODO Auto-generated method stub
				rpcService.createExercise(exercise, topic, cb);
			}
		}.retry(3);
	}

	@Override
	public void go(HasWidgets container) {
		// TODO Auto-generated method stub
		container.clear();
		container.add(display.asWidget());
	}

	@Override
	public void go() {
		// TODO Auto-generated method stub

	}

	void getExerciseFromDisplay()
	{
		if(exercise == null)
			exercise = new ExerciseDTO();
		try {
			exercise.setAnswer(display.getCorrectAnsIntBox().getValue());
		} catch (Exception e) {
			// TODO: handle exception
			exercise.setAnswer(0);
		}
		exercise.setQuestion(display.getQuestionHtml().getHTML());
		
		ArrayList<String> answers = new ArrayList<String>();
		answers.add(display.getAnswer1Html().getHTML());
		answers.add(display.getAnswer2Html().getHTML());
		answers.add(display.getAnswer3Html().getHTML());
		answers.add(display.getAnswer4Html().getHTML());
		
		exercise.setChooses(answers);
	}
}
